package com.meituan.catering.management.order.biz.model.converter;

import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.order.api.http.model.response.CateringOrderDetailHttpResponse;
import com.meituan.catering.management.order.api.http.model.response.CateringOrderPageHttpResponse;
import com.meituan.catering.management.order.biz.model.CateringOrderBO;
import com.meituan.catering.management.order.biz.model.CateringOrderItemAccessoryBO;
import com.meituan.catering.management.order.biz.model.CateringOrderItemBO;

import java.util.stream.Collectors;

/**
 * 从其他数据模型向订单Http模型的转换器
 */
public abstract class CateringOrderHttpVOConverter {
    /**
     * 根据PageBO转VO
     * @return  CateringOrderPageHttpResponse
     */
    public static CateringOrderPageHttpResponse fromPageBO(PageBO<CateringOrderBO> pageBO){
        CateringOrderPageHttpResponse response=new CateringOrderPageHttpResponse();
        response.setPageIndex(pageBO.getPageIndex());
        response.setPageSize(pageBO.getPageSize());
        response.setTotalCount(pageBO.getTotalCount());
        response.setTotalPageCount(pageBO.getTotalPageCount());
        response.getRecords().addAll(pageBO.getRecords().stream().map(CateringOrderHttpVOConverter::fromPageBORecord).collect(Collectors.toList()));
        return response;
    }
    /**
     * 根据CateringOrderBO转VO
     */
    public static CateringOrderPageHttpResponse.Record fromPageBORecord(CateringOrderBO cateringOrderBO){
        CateringOrderPageHttpResponse.Record record=new CateringOrderPageHttpResponse.Record();
        record.setCustomerCount(cateringOrderBO.getCustomerCount());
        record.setStatus(cateringOrderBO.getStatus());
        record.setTableNo(cateringOrderBO.getTableNo());
        record.setTenantId(cateringOrderBO.getTenantId());
        record.setComment(cateringOrderBO.getComment());
        record.setId(cateringOrderBO.getId());
        record.setVersion(cateringOrderBO.getVersion());
        record.setTotalPrice(cateringOrderBO.getTotalPrice());
        record.getAuditing().setCreatedAt(cateringOrderBO.getAuditing().getCreatedAt());
        record.getAuditing().setCreatedBy(cateringOrderBO.getAuditing().getCreatedBy());
        record.getAuditing().setLastModifiedAt(cateringOrderBO.getAuditing().getLastModifiedAt());
        record.getAuditing().setLastModifiedBy(cateringOrderBO.getAuditing().getLastModifiedBy());
        record.getShopSnapshotOnPlace().setName(cateringOrderBO.getShopName());
        record.getShopSnapshotOnPlace().setId(cateringOrderBO.getShopId());
        record.getShopSnapshotOnPlace().setBusinessNo(cateringOrderBO.getShopBusinessNo());
        return record;
    }

    public static CateringOrderDetailHttpResponse fromCateringOrderBO(CateringOrderBO cateringOrderBO) {
        CateringOrderDetailHttpResponse response=new CateringOrderDetailHttpResponse();
        response.setComment(cateringOrderBO.getComment());
        response.setCustomerCount(cateringOrderBO.getCustomerCount());
        response.setStatus(cateringOrderBO.getStatus());
        response.setId(cateringOrderBO.getId());
        response.setTableNo(cateringOrderBO.getTableNo());
        response.setVersion(cateringOrderBO.getVersion());
        response.setTenantId(cateringOrderBO.getTenantId());
        response.setTotalPrice(cateringOrderBO.getTotalPrice());
        response.getAuditing().setCreatedBy(cateringOrderBO.getAuditing().getCreatedBy());
        response.getAuditing().setCreatedAt(cateringOrderBO.getAuditing().getCreatedAt());
        response.getAuditing().setLastModifiedBy(cateringOrderBO.getAuditing().getLastModifiedBy());
        response.getAuditing().setLastModifiedAt(cateringOrderBO.getAuditing().getLastModifiedAt());
        response.getShopSnapshotOnPlace().setName(cateringOrderBO.getShopName());
        response.getShopSnapshotOnPlace().setId(cateringOrderBO.getShopId());
        response.getShopSnapshotOnPlace().setBusinessNo(cateringOrderBO.getShopBusinessNo());
        response.getBilling().setPaid(cateringOrderBO.getBillingPaid());
        response.getBilling().setPromotion(cateringOrderBO.getBillingPromotion());
        response.getBilling().setPaymentChannel(cateringOrderBO.getBillingPaymentChannel());
        /*
         * 子项转换
         */
        response.getItems().addAll(cateringOrderBO.getCateringOrderItemBOList().stream().map(CateringOrderHttpVOConverter::fromItemBO).collect(Collectors.toList()));


        return response;
    }

    /**
     * 订单子项BO转DO
     */
    public static CateringOrderDetailHttpResponse.Item fromItemBO(CateringOrderItemBO cateringOrderItemBO){
        CateringOrderDetailHttpResponse.Item item=new CateringOrderDetailHttpResponse.Item();
        item.setId(cateringOrderItemBO.getId());
        item.setStatus(cateringOrderItemBO.getStatus());
        item.setVersion(cateringOrderItemBO.getVersion());
        item.setSeqNo(cateringOrderItemBO.getSeqNo());
        /*
         * 转换做法
         */
        item.getProductMethodSnapshotOnPlace().setId(cateringOrderItemBO.getProductMethodSnapshotOnPlace().getId());
        item.getProductMethodSnapshotOnPlace().setGroupName(cateringOrderItemBO.getProductMethodSnapshotOnPlace().getGroupName());
        item.getProductMethodSnapshotOnPlace().setName(cateringOrderItemBO.getProductMethodSnapshotOnPlace().getName());
        /*
         * 转换商品信息
         */
        item.getProductSnapshotOnPlace().setId(cateringOrderItemBO.getProductSnapshotOnPlace().getId());
        item.getProductSnapshotOnPlace().setName(cateringOrderItemBO.getProductSnapshotOnPlace().getName());
        item.getProductSnapshotOnPlace().setUnitOfMeasure(cateringOrderItemBO.getProductSnapshotOnPlace().getUnitOfMeasure());
        item.getProductSnapshotOnPlace().setUnitPrice(cateringOrderItemBO.getProductSnapshotOnPlace().getUnitPrice());
        /*
         * 转换数量
         */
        item.getQuantity().setLatest(cateringOrderItemBO.getQuantity().getLatest());
        item.getQuantity().setOnPlace(cateringOrderItemBO.getQuantity().getOnPlace());
        item.getQuantity().setOnProduce(cateringOrderItemBO.getQuantity().getOnProduce());
        /*
         * 转换加料
         */
        item.getAccessories().addAll(cateringOrderItemBO.getAcc().stream().map(CateringOrderHttpVOConverter::fromItemAccBO).collect(Collectors.toList()));
        return item;

    }
    /**
     * 订单子项加料 BO转VO
     */
    public static CateringOrderDetailHttpResponse.Item.Accessory fromItemAccBO(CateringOrderItemAccessoryBO itemAccessoryBO)
    {
        CateringOrderDetailHttpResponse.Item.Accessory accessory=new CateringOrderDetailHttpResponse.Item.Accessory();
        accessory.setId(itemAccessoryBO.getId());
        accessory.setStatus(itemAccessoryBO.getStatus());
        accessory.setSeqNo(itemAccessoryBO.getSeqNo());
        accessory.setVersion(itemAccessoryBO.getVersion());
        /*
         * 转换数量
         */
        accessory.getQuantity().setLatest(itemAccessoryBO.getQuantity().getLatest());
        accessory.getQuantity().setOnPlace(itemAccessoryBO.getQuantity().getOnPlace());
        accessory.getQuantity().setOnProduce(itemAccessoryBO.getQuantity().getOnProduce());
        /*
         * 转换加料信息
         */
        accessory.getProductAccessorySnapshotOnPlace().setGroupName(itemAccessoryBO.getProductAccessorySnapshotOnPlace().getGroupName());
        accessory.getProductAccessorySnapshotOnPlace().setId(itemAccessoryBO.getProductAccessorySnapshotOnPlace().getId());
        accessory.getProductAccessorySnapshotOnPlace().setName(itemAccessoryBO.getProductAccessorySnapshotOnPlace().getName());
        accessory.getProductAccessorySnapshotOnPlace().setUnitPrice(itemAccessoryBO.getProductAccessorySnapshotOnPlace().getUnitPrice());
        accessory.getProductAccessorySnapshotOnPlace().setUnitOfMeasure(itemAccessoryBO.getProductAccessorySnapshotOnPlace().getUnitOfMeasure());
        return accessory;

    }
}
